package server;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.sun.jersey.spi.container.servlet.ServletContainer;

import util.PropertiesUtils;

public class JettyServer {
	
	private final static Logger logger = LogManager.getLogger(JettyServer.class);
	private final static String PORT = "server.port";
	
	
	private Server server;
	
	public JettyServer() {
		int port = Integer.parseInt(PropertiesUtils.getProperties(PORT));
		server = new Server(port);
		ServletHolder servlet = new ServletHolder(ServletContainer.class);
	    servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
	    servlet.setInitParameter("com.sun.jersey.config.property.packages", "jersey");
	    servlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
	    ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
	    handler.setContextPath("/");
	    handler.addServlet(servlet, "/*");
	    server.setHandler(handler);
	}
	
	public void server() {
		try {
			server.start();
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	public static void main(String[] args) {
		new JettyServer().server();
	}

}
